$(function(){
    console.log('已准备就绪！');

    $('#btnQuery').click(function(){
        let keyword = $('[name=keyword]')
        let obj = {
            keyword,
        }
        $.post('/Product/Query',obj,function(res){
            alert('查询成功')
            console.log(obj);
            res.forEach(res => {
                let html = 
                `
                <tr  id="tbRow">
                <td>${item.id}</td>
                <td>${item.productName}</td>
                <td>${item.supplier}</td>
                <td>${item.price}</td>
                <td>${item.stock}</td>
                <td>${item.remarks}</td>
                <td>
                    <input type="button" onclick="btnEdit(${item.id})" value="编辑" id="btnEdit">
                    <input type="button" onclick="btnDel(${item.id})" value="删除" id="btnDel">
                </td>
            </tr>
                `
                $('.tbRow').html('');
                $('#tbData').append(html);
            });
        })

    })

    // $('#btnEdit').click(function(){

    // })

    // $('#btnDel').click(function(){

    // })

    $('#btnSave').click(function(){
        let id = $('[name=id]').val();
        let ProductName = $('[name=ProductName]').val();
        let Supplier = $('[name=Supplier]').val();
        let Price = $('[name=Price]').val();
        let Stock = $('[name=Stock]').val();
        let Remarks = $('[name=Remarks]').val();
        let obj = {
            id,
            ProductName,
            Supplier,
            Price,
            Stock,
            Remarks
        }
        console.log(obj);
        $.post('/Product/Save',obj,function(){
            alert('保存成功！')
            window.location.href=`/Product/index`
        })
        
    })

    $('#btnCancel').click(function(){
        window.location.href=`/Product/index`
    })

})

function btnEdit (id)
{
    window.location.href=`/Product/CreateOrEdit/${id}`
}
function btnDel(id)
{
    var mag = '确定要删除吗？'
    if(confirm(mag))
    {
        window.location.href=`/Product/Delete/${id}`
    }
}